﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using System.Diagnostics;
using LotteryVote.Entity;

namespace LotteryVote
{
    public partial class NumLengthManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCompany();
                BindNumLength();
            }
        }

        private void BindCompany()
        {
            CompanyTypeFacade companyTypeFacade = new CompanyTypeFacade();
            ddl_companyType.DataSource = companyTypeFacade.GetAllType();
            ddl_companyType.DataTextField = CompanyTypeModel.TYPENAME;
            ddl_companyType.DataValueField = CompanyTypeModel.ID;
            ddl_companyType.DataBind();
        }
        private void BindNumLength()
        {
            NumLenFacade numLenFacade = new NumLenFacade();
            ddl_numlen.DataSource = numLenFacade.GetAllNumLength();
            ddl_numlen.DataTextField = NumLength.LENNAME;
            ddl_numlen.DataValueField = NumLength.LENID;
            ddl_numlen.DataBind();
        }

        protected void btn_AddNumLen_Click(object sender, EventArgs e)
        {
            NumLenFacade numLenFacade = new NumLenFacade();
            int len = int.Parse(txt_len.Text);
            if (numLenFacade.IsExistNumLength(len))
            {
                ltl_message.Text = "该号码长度已存在!";
                return;
            }
            numLenFacade.AddNumLength(len, txt_lenName.Text, string.Empty);
            ltl_message.Text = "添加号码长度成功!";
        }

        protected void btn_DelteNumLen_Click(object sender, EventArgs e)
        {
            NumLenFacade numLenFacade = new NumLenFacade();
            int lenId = int.Parse(txt_lenId.Text);
            numLenFacade.RemoveNumLengthByLenId(lenId);
            ltl_message.Text = "删除成功!";
        }

        protected void btn_RemoveNumLen_Click(object sender, EventArgs e)
        {
            NumLenFacade numLenFacade = new NumLenFacade();
            int len = int.Parse(txt_len.Text);
            numLenFacade.RemoveNumLengthByLength(len);
            ltl_message.Text = "删除成功!";
        }

        protected void btn_GetAllNumLength_Click(object sender, EventArgs e)
        {
            NumLenFacade numLenFacade = new NumLenFacade();
            gv_numLength.DataSource = numLenFacade.GetAllNumLength();
            gv_numLength.DataBind();
        }

        protected void btn_GetAll_Click(object sender, EventArgs e)
        {
            NumLenFacade numLenFacade = new NumLenFacade();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var numLen = numLenFacade.GetNumLengthByCompanyType(1);
            ltl_message.Text = sw.ElapsedMilliseconds.ToString();
            sw.Stop();
        }

        protected void btn_addSupport_Click(object sender, EventArgs e)
        {
            int companyTyped = int.Parse(ddl_companyType.SelectedValue);
            int numlenId = int.Parse(ddl_numlen.SelectedValue);
            NumLenFacade numLenFacade = new NumLenFacade();
            if (numLenFacade.IsCompanyTypeSupportNumLen(companyTyped, numlenId))
            {
                ltl_message.Text = "该记录已存在!";
                return;
            }
            numLenFacade.AddCompanyTypeSupportNumLen(companyTyped, numlenId, int.Parse(txt_count.Text));
            ltl_message.Text = "添加成功!";
        }

        protected void btn_updateSupport_Click(object sender, EventArgs e)
        {
            int companyTypeId = int.Parse(ddl_companyType.SelectedValue);
            int numlenId = int.Parse(ddl_numlen.SelectedValue);
            NumLenFacade numLenFacade = new NumLenFacade();
            numLenFacade.UpdateCompanyTypeSupportNumLen(companyTypeId, numlenId, int.Parse(txt_count.Text));
            ltl_message.Text = "修改成功!";
        }

        protected void btn_delSupport_Click(object sender, EventArgs e)
        {
            int companyTypeId = int.Parse(ddl_companyType.SelectedValue);
            int numlenId = int.Parse(ddl_numlen.SelectedValue);
            NumLenFacade numLenFacade = new NumLenFacade();
            numLenFacade.RemoveCompanyTypeSupportNumLen(companyTypeId, numlenId);
            ltl_message.Text = "删除成功!";
        }

        protected void btn_getSuport_Click(object sender, EventArgs e)
        {
            int companyTypeId = int.Parse(ddl_companyType.SelectedValue);
            NumLenFacade numLenFacade = new NumLenFacade();
            gv_supportNumlen.DataSource = numLenFacade.GetNumLengthByCompanyType(companyTypeId);
            gv_supportNumlen.DataBind();
        }
    }
}